<!-- 侧边工具栏 -->
<template>
  <div class="aside">
    <!-- 侧边栏 -->
    <ul class="piece">
      <li v-for="(item, index) of piece" :key="index" @click="onChange(item)">
        <div class="item" :class="{active: current.name === item.name}">
          <i class="iconfont" :class="item.icon"></i>
          <em>{{item.name}}</em>
        </div>
      </li>
    </ul>
    <div class="content" v-if="current.name">
      <!-- 侧边栏项目详情 -->
      <component :is="currentComponent"></component>
      <!-- 收起 -->
      <div class="close" @click="onClose">
        <i class="el-icon-arrow-left"></i>
      </div>
    </div>
  </div>
</template>

<script>
import {
  typeConstants
} from '../../config'
import { AsideBackground, AsideImage, AsideFont } from './components'
export default {
  name: 'Aside',
  components: {
    AsideBackground,
    AsideImage,
    AsideFont
  },
  data () {
    return {
      current: {}, // 当前选中的侧边栏
      currentComponent: {}, // 当前选中的侧边栏组件
      piece: [ // 侧边栏
        {
          icon: 'icon-001',
          name: '模板',
          value: typeConstants.TEMPLATE
        },
        {
          icon: 'icon-002',
          name: '字体',
          value: typeConstants.FONT
        },
        {
          icon: 'icon-003',
          name: '图片',
          value: typeConstants.IMAGE
        },
        {
          icon: 'icon-004',
          name: '元素',
          value: typeConstants.ELEMENT
        },
        {
          icon: 'icon-005',
          name: '背景',
          value: typeConstants.BACKGROUND
        }
      ]
    }
  },
  methods: {
    // 选中侧边栏
    onChange (row) {
      this.current = row
      switch (row.value) { // 匹配选中的组件, 然后加载
        case typeConstants.BACKGROUND:
          this.currentComponent = AsideBackground
          break
        case typeConstants.IMAGE:
          this.currentComponent = AsideImage
          break
        case typeConstants.FONT:
          this.currentComponent = AsideFont
          break
        default:
          this.currentComponent = {}
          break
      }
    },
    onClose () {
      this.current = {}
      this.currentComponent = {}
    }
  }
}
</script>

<style lang="less" scoped>
.aside {
  background: #fff;
  width: 65px;
  position: relative;
  z-index: 90;
  box-shadow: 3px 0 4px rgba(0, 0, 0, .1);
  .piece {
    .item {
      text-align: center;
      padding: 10px 0;
      cursor: pointer;
      margin-bottom: 1px;
      &.active {
        color: #e23738;
        background: #fafafa;
      }
      &:hover {
        background: #fafafa;
      }
      i, em {
        display: block;
        font-size: normal
      }
      em {
        font-size: 12px;
        padding-top: 6px;
      }
    }
  }
  .content {
    position: absolute;
    padding: 0 10px;
    left: 65px;
    top: 0;
    background: #fff;
    width: 330px;
    height: 100%;
    box-shadow: 3px 3px 10px rgba(0, 0, 0, .1);
    z-index: 50;
    // 侧边栏展开后的拓展项
    & ::v-deep .expect {
      height: 100%;
      display: flex;
      align-items: center;
      text-align: center;
      color: #999;
      span {
        flex: 1;
      }
    }
    .close {
      position: absolute;
      left: 100%;
      top: 50%;
      width: 16px;
      height: 60px;
      border-radius: 0 5px 5px 0;
      display: flex;
      align-items: center;
      text-align: center;
      background: #fff;
      transform: translate3d(0, -50%, 0);
      box-shadow: 3px 3px 10px rgba(0, 0, 0, .1);
      cursor: pointer;
    }
    // 侧边栏展开通用模板样式
    & ::v-deep .aside-template {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    & ::v-deep .title {
      font-weight: bold;
      padding: 10px 0;
      line-height: 1;
    }
    & ::v-deep .list {
      overflow: auto;
      flex: 1;
    }
  }
}
</style>